<?php


namespace fast;

use think\Db;

class DeliveryNoteGenerator
{
    private $saveDir = ROOT_PATH . 'public' . DS . 'order'. DS;

    public function __construct()
    {
       
    }


    public function addOrder($id)
    {
       
        $order = Db::table('fa_pifa_order')->find($id);


        if (empty($order)) {
            logs('order_create','订单ID----'.$id.'-----订单不存在');     
            return;
        }


        $map2['order_id'] = $id;
        $OrderitemModel = new \app\admin\model\pifa\Orderitem;

        $order_item = $OrderitemModel::with(['goodsitem'])->where($map2)->select();

        $address = Db::table('fa_user_address')->find($order['address_id']);


        $orderData = [
            'order_no' => $order['order_no'],
            'date' => $order['peisong_time'],
            'customer' => $address['md_name'],
            'address' => $order['address'],
            'phone' => $order['phone'],
            'goods_price' => $order['goods_price'],
            'peisong' => [],
            'items' => [
            ],
        ];
        $total = 0;
        foreach ($order_item as $key => $value) {

            $orderData['items'][$key] = [];
            $orderData['items'][$key]['name'] = $value['goods_name'];
            $orderData['items'][$key]['spec'] = !empty($value['name']) ? $value['name'] : "";
            $orderData['items'][$key]['qty'] = $value['goods_num'];
            $orderData['items'][$key]['price'] = $value['price'];
            $orderData['items'][$key]['danjia'] = bcdiv($value['price'], $value['goods_num'],2);

        }

        if (!empty($order['peisong_user_id'])) {
            $peisong = Db::table('fa_user')->find($order['peisong_user_id']);

            if (!empty($peisong)) {
                $orderData['peisong'] = $peisong;
            }
  
        }

        return $this->generate($orderData);

    }

    public function generate(array $order): string
    {
        if (empty($order['order_no'])) {
            throw new Exception("订单号不能为空");
        }

        if (!empty($order['peisong'])) {
            $html = $this->renderHtmlPeisong($order);
        }else{
            $html = $this->renderHtml($order);
        }

       

        // 文件路径
        $filename = $this->saveDir . $order['order_no'] . '.html';

        // 写入文件
        file_put_contents($filename, $html);

        return $filename;
    }

    private function renderHtml(array $order): string
    {
        ob_start();
        $total = 0;

      
        ?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>冻宝云销售单 - <?= $order['order_no'] ?></title>
    <style>
        body { font-family: "Arial", "Microsoft YaHei"; margin: 10px;font-size:12px }
        h1 { text-align: center;font-weight:400 }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th, td { border: 1px solid #333; padding: 2px; text-align: center; }
        .info { margin-top: 20px; }
         p{margin: 0;line-height: 1.5}
         .item{display: flex;justify-content: space-between;}
    </style>
</head>
<body>
    <h1>冻宝云销售单</h1>
    <div class="info">
        <div class="item">
            <p>发货日期：<?= $order['date'] ?? date('Y-m-d') ?></p>
            <p>客户名称：<?= $order['customer'] ?? '' ?></p>
        </div>
        <div class="item">
            <p>订单号：<?= $order['order_no'] ?></p>
            <p>联系电话：<?= $order['phone'] ?? '' ?></p>
        </div>
        <div class="item">
            <p>配送员信息：</p>
           <p>收货地址：<?= $order['address'] ?? '' ?></p>
        </div>
    </div>

    <table>
        <thead>
            <tr>
                <td>序号</td>
                <td>商品名称</td>
                <td>规格</td>
                <td>数量</td>
                <td>单价</td>
                <td>小计</td>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($order['items'] as $index => $item): ?>
               
                <tr>
                    <td><?= $index + 1 ?></td>
                    <td><?= $item['name'] ?></td>
                    <td><?= $item['spec'] ?></td>
                    <td><?= $item['qty'] ?></td>
                    <td><?= formatCurrency($item['danjia']) ?></td>
                    <td><?= formatCurrency($item['price']) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="5">合计</td>
                <td><?= formatCurrency($order['goods_price']) ?></td>
            </tr>
        </tfoot>
    </table>
</body>
</html>

        <?php
        return ob_get_clean();
    }



       private function renderHtmlPeisong(array $order): string
    {
        ob_start();
        $total = 0;

       
        ?>

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>冻宝云销售单 - <?= $order['order_no'] ?></title>
    <style>
        body { font-family: "Arial", "Microsoft YaHei"; margin: 10px;font-size:12px }
        h1 { text-align: center;font-weight:400 }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th, td { border: 1px solid #333; padding: 2px; text-align: center; }
        .info { margin-top: 20px; }
        p{margin: 0;line-height: 1.5}
        .item{display: flex;justify-content: space-between;}
    </style>
</head>
<body>
    <h1>冻宝云销售单</h1>
    <div class="info">
    
         <div class="item">
            <p>发货日期：<?= $order['date'] ?? date('Y-m-d') ?></p>
            <p>客户名称：<?= $order['customer'] ?? '' ?></p>
        </div>
        <div class="item">
            <p>订单号：<?= $order['order_no'] ?></p>
            <p>联系电话：<?= $order['phone'] ?? '' ?></p>
        </div>
        <div class="item">
           <p>配送员信息：<?= $order['peisong']['nickname'] ?? '' ?>--------<?= $order['peisong']['mobile'] ?? '' ?></p>
           <p>收货地址：<?= $order['address'] ?? '' ?></p>
        </div>
       
    </div>

    <table>
        <thead>
            <tr>
                <td>序号</td>
                <td>商品名称</td>
                <td>规格</td>
                <td>数量</td>
                <td>单价</td>
                <td>小计</td>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($order['items'] as $index => $item): ?>
               
                <tr>
                    <td><?= $index + 1 ?></td>
                    <td><?= $item['name'] ?></td>
                    <td><?= $item['spec'] ?></td>
                    <td><?= $item['qty'] ?></td>
                    <td><?= formatCurrency($item['danjia']) ?></td>
                    <td><?= formatCurrency($item['price']) ?></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
        <tfoot>
            <tr>
                <td colspan="5">合计</td>
                <td><?= formatCurrency($order['goods_price']) ?></td>
            </tr>
        </tfoot>
    </table>
</body>
</html>

        <?php
        return ob_get_clean();
    }
}
